package HLLib.userControl.HLPanelPickUp;

import javax.microedition.lcdui.Image;

import HLLib.base.HLGraphics;
import HLLib.base.HLGraphics_H;
import HLLib.base.HLImage;
import HLLib.base.HLMath;
import HLLib.control.HLPanel;
import HLLib.control.HLPhotoFrame;
import HLLib.control.HLTextPageOld;
import HLLib.handfere.HLUIRes;
import HLLib.handfere.HLCommunityData.HLPoto.HLPhoto;

public class HLPhotoPanel extends HLPanel {
    public static final int PHOTO_X = 3;

    public static final int PHOTO_Y = 10;

    public static final int TEXT_Y = 2;

    public HLPhoto photo;

    HLPhotoFrame photoFrame;

    public int selectColor = 0xffff00;

    public int color = 0xffecf3fd;

    Image img;

    Image sex[] = HLUIRes.uires.GetSex();

    HLTextPageOld textpage;

    public HLPhotoPanel(HLPhoto photo, int width, Image img) {
        this.photo = photo;
        this.width = (short) width;
        this.img = img;
        this.backcolor = -1;
        this.tabelStop = true;
        photoFrame = new HLPhotoFrame(photo.img);
        photoFrame.y = PHOTO_Y;
        photoFrame.x = HLMath.GetPosAdjustableH(photoFrame.width, 0, width, HCENTER);
        photoFrame.imagePosStyle = HCENTER_VCENTER;
        Add(photoFrame);

        textpage = new HLTextPageOld(width - TEXT_Y * 2);
        textpage.textColor = 0;
        textpage.SetText1(photo.photoDes);
        this.height = (short) (PHOTO_Y + photoFrame.height + TEXT_Y + textpage.height + TEXT_Y);
        needDownload = photo.img.GetImage1() == null;
        oriHeight = photo.img.GetHeight();
    }

    int oriHeight = 0;

    boolean haveDownloadOver = false;

    boolean needDownload = true;

    public void AdjustSize() {
        if (parent != null) {
            parent.AdjustSize();
        }
    }

    public void Logic() {

        if (needDownload) {
            if (photo.img != null && !haveDownloadOver) {
                if (photo.img.GetImage1() == null) {
                    haveDownloadOver = false;
                } else {
                    haveDownloadOver = true;
                    this.height = PHOTO_Y + photo.img.GetHeight() + TEXT_Y + textpage.height + TEXT_Y;
                    AdjustSize();
                    GetUiRoot().Refresh();
                    photoFrame.AjustPos(0, photoFrame.y, HLLib.base.HLScreen.width, 0, HCENTER_TOP);
                }
            }
        }
        super.Logic();
        if (IsFocus()) {
            RefreshEvent(GetScreenX(), GetScreenY(), GAME_BUTTON_ACTION);
        }
    }

    public void Refresh() {
        super.Refresh();

    }

    public void Render(HLGraphics g) {
        super.Render(g);
        int screenX = GetScreenX();
        int screenY = GetScreenY();
        g.FillRect(screenX, screenY, width, height, 0xffffff);
        if (IsFocus()) {
            // g.DrawImageFillRect(new HLImage(img[1]), screenX, screenY, width,
            // img[1].getHeight(), HLGraphics_H.TRANS_NONE);
            // g.DrawImageFillRect(new HLImage(img[0]), screenX, screenY +
            // height - img[0].getHeight(), width, img[1].getHeight(),
            // HLGraphics_H.TRANS_NONE);
            g.FillRect(screenX, screenY, width, height, HLPanelGroup.SELECTED_COLOR);// d0d9f2);//0xd0ebf2);
            g.DrawImage(new HLImage(img), screenX + width - img.getWidth() - 5, screenY + (height - img.getHeight() >> 1), HLGraphics_H.TRANS_NONE);
        } else {

        }
        int offsetY = screenY + photoFrame.y + photoFrame.height + TEXT_Y;

        // g.DrawStringAdjustable( photo.photoDes, 0xff4b4b4b, 0, screenX,
        // offsetY, width, height, HLUtil.TEXT_STYLE_NORMAL,
        // HLUtil.HCENTER_TOP);//
        textpage.Render(g, TEXT_Y, offsetY);
        photoFrame.Render(g);

    }

}
